package com.yami.shop.distribution.api.linstener;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.app.param.UserInfoParam;
import com.yami.shop.bean.event.UpdateDistributionUserEvent;
import com.yami.shop.distribution.common.model.DistributionUser;
import com.yami.shop.distribution.common.service.DistributionUserService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 商家转账事件监听
 * @author lhd
 */
@Component("updateDistributionUserListener")
@AllArgsConstructor
public class UpdateDistributionUserListener {

    private final DistributionUserService distributionUserService;


    @EventListener(UpdateDistributionUserEvent.class)
    public void updateDistributionUserEventHandle(UpdateDistributionUserEvent event) {
        UserInfoParam userInfoParam = event.getUserInfoParam();
        DistributionUser distributionUser = distributionUserService.getOne(new LambdaQueryWrapper<DistributionUser>().eq(DistributionUser::getUserId, event.getUserId()));
        if(Objects.isNull(distributionUser)){
            return;
        }
        distributionUser.setNickName(StrUtil.isBlank(userInfoParam.getNickName())? distributionUser.getNickName():userInfoParam.getNickName());
        distributionUser.setPic(StrUtil.isBlank(userInfoParam.getAvatarUrl())?distributionUser.getPic() :userInfoParam.getAvatarUrl());
        distributionUserService.updateById(distributionUser);
        distributionUserService.removeCacheByUserIdAndShopId(distributionUser.getUserId(),distributionUser.getShopId());
    }



}
